<?php
namespace Admin\Model;
use Think\Model;
class CommentModel extends Model{
    // 发表评论时表单中允许提交的字段
    protected $insertFields = array('content','star','goods_id');
    protected $_validate = array(
            array('content', 'require', '评论的内容不能为空！', 1, 'regex', 3),
            array('star', '/^[1-5]$/', '分值必须是1-5之间的数字！', 1),
    );
    protected function _before_insert(&$data, $option) {
        $data['addtime'] = time();
        $data['member_id']= session('mid');
        //处理印象的数据
        $yx = I('post.yx');
        if($yx){
            $impModel = M('Impression');
            //先统一字符串的逗号 都用英文
            $yx = str_replace('，', ',', $yx);
            //转成数组
            $yx= explode(',', $yx);
            foreach ($yx as $k => $v) {
                //判断有没有这个印象
                $has=$impModel->field('id')->where(array(
                    'goods_id'=>array('eq',$data['goods_id']),
                    'imp_name'=>array('eq',$v),
                ))->find();
                if($has){
                    $impModel->where('id='.$has['id'])->setInc('imp_count');
                }else{
                    $impModel->add(array(
                        'goods_id'=>$data['goods_id'],
                        'imp_name'=>$v,
                    ));
                }
            }
        }
    }
}

